/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.dispensedrug.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.th.supcom.pds.auditprescription.client.model.PdsPrescCombineModel;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.client.widget.PdsGrid;

/**
 * 核对发药处方面板
 * 
 * @author 杨冲
 */
public class DispenseDrugRecipePanel extends ContentPanel
{

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private PdsGrid <PdsSupplyModel> m_grid;

    /**
     * 构造函数
     */
    public DispenseDrugRecipePanel ()
    {
        initUI ();
    }

    private void createGrid ()
    {
        m_grid = new PdsGrid <PdsSupplyModel> ()
        {

            @Override
            public ColumnModel createColumnModel ()
            {
                return getColumnModel ();
            }
        };
        add (m_grid);
    }

    /**
     * 创建金额Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createPriceRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config, final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                Double totalPrice = model.get ("totalPrice");
                if (null != totalPrice)
                {
                    totalPrice = SimpleMethod.retainDoubleTwo (totalPrice);
                }
                return "<span style='float:right'>" + totalPrice + "</span>";
            }
        };
    }

    /**
     * 创建Grid列信息
     * 
     * @return
     */
    private ColumnModel getColumnModel ()
    {
        final List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();
        ColumnConfig column = null;
        column = new ColumnConfig ("window", "", 20);
        configs.add (column);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                if (model.get ("windowFlag") != null)
                {
                    return "<div style='background-color:red;height:16px' title='需要审核'></div>";
                }
                return "";

            }
        });
        column = new ColumnConfig ("prescNo", "处方号", 100);
        configs.add (column);
        column = new ColumnConfig ("patiName", "姓名", 60);
        configs.add (column);
        column = new ColumnConfig ("totalPrice", "金额", 70);
        column.setRenderer (createPriceRenderer ());
        configs.add (column);
        return new ColumnModel (configs);
    }

    /**
     * 获取PdsGrid
     * 
     * @return
     */
    public PdsGrid <PdsSupplyModel> getGridPanel ()
    {
        return m_grid;
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setHeading ("收方待发药");
        this.setLayout (new FitLayout ());

        createGrid ();
    }
}
